package com.yanqu.road.dao.impl.zhaoshang;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.zhaoshang.ZsTopOfRank;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ZsTopRankDaoImpl extends TempDataObjectDao<ZsTopOfRank> {


    @Override
    protected boolean addData(ZsTopOfRank data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_zs_top_rank(weekId, bigRankType, rankType, userId, userBaseInfo, value, lastUpdateTime, param) values (?,?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getWeekId());
            call.setInt(i++, data.getBigRankType());
            call.setInt(i++, data.getRankType());
            call.setLong(i++, data.getUserId());
            call.setString(i++, data.getUserBaseInfoJsonStr());
            call.setLong(i++, data.getValue());
            call.setLong(i++, data.getLastUpdateTime());
            call.setString(i++, data.getParam());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(ZsTopOfRank data) {
        return false;
    }

    @Override
    protected ZsTopOfRank parseData(ResultSet rs) throws SQLException {
        ZsTopOfRank data = new ZsTopOfRank();
        data.setWeekId(rs.getInt("weekId"));
        data.setBigRankType(rs.getInt("bigRankType"));
        data.setRankType(rs.getInt("rankType"));
        data.setUserId(rs.getLong("userId"));
        String baseUserInfoStr = rs.getString("userBaseInfo");
        if(null == baseUserInfoStr){
            baseUserInfoStr = "";
        }
        UserBaseInfo userBaseInfo = JSON.parseObject(baseUserInfoStr, new TypeReference<UserBaseInfo>() {});
        if(null != userBaseInfo) {
            data.setUserBaseInfo(userBaseInfo);
        }
        data.setValue(rs.getLong("value"));
        data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
        data.setParam(rs.getString("param"));
        return data;
    }

    public Map<Integer, Map<Integer, ZsTopOfRank>> getWeekTopRankMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, ZsTopOfRank>> dataMap = new ConcurrentHashMap<>();
        String sql = "select * from t_u_zs_top_rank where weekId = ?";
        try {
            int i = 1;
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                ZsTopOfRank data = parse(rs);
                if(dataMap.get(data.getWeekId()) == null){
                    dataMap.put(data.getWeekId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getWeekId()).put(data.getRankType(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }
}
